package com.zhuanju.core.utils

import android.util.Log

/**
 * @author zqq on 2018/10/24
 */
object ZLog {


    var isDebug = false // 是否需要打印bug，可以在application的onCreate函数里面初始化
    val TAG: String = "ZLog"

    /*********----- 下面四个是默认tag的函数 --------- */
    fun v(msg: String) {
        if (!isDebug) {
            return
        }
        val stackTraceElement = getTargetStackTraceElement()
        Log.v(TAG, "(${stackTraceElement.fileName}:${stackTraceElement.lineNumber})")
        Log.v(TAG, msg)
    }

    fun i(msg: String) {
        if (!isDebug)
            return
        val stackTraceElement: StackTraceElement = getTargetStackTraceElement()
        Log.i(TAG, "(${stackTraceElement.fileName}:${stackTraceElement.lineNumber})")
        Log.i(TAG, msg)
    }

    fun d(msg: String) {
        if (!isDebug) {
            return
        }
        val stackTraceElement = getTargetStackTraceElement()
        Log.d(TAG, "(${stackTraceElement.fileName}:${stackTraceElement.lineNumber})")
        Log.d(TAG, msg)
    }

    fun e(msg: String) {
        if (!isDebug) {
            return
        }
        val stackTraceElement = getTargetStackTraceElement()
        Log.e(TAG, "(${stackTraceElement.fileName}:${stackTraceElement.lineNumber})")
        Log.e(TAG, msg)
    }

    /***************--- 下面是传入自定义tag的函数------**********/
    fun v(tag: String, msg: String) {
        if (!isDebug) {
            return
        }
        val stackTraceElement = getTargetStackTraceElement()
        Log.v(tag, "(${stackTraceElement.fileName}:${stackTraceElement.lineNumber})")
        Log.v(tag,msg)
    }

    fun i(tag: String, msg: String) {
        if (!isDebug)
            return
        val targetStackTraceElement = getTargetStackTraceElement()
        Log.i(tag, "(" + targetStackTraceElement.fileName + ":" + targetStackTraceElement.lineNumber + ")")
        Log.i(tag, msg)
    }

    fun d(tag: String, msg: String) {
        if (!isDebug)
            return
        val targetStackTraceElement = getTargetStackTraceElement()
        Log.d(tag, "(" + targetStackTraceElement.fileName + ":" + targetStackTraceElement.lineNumber + ")")
        Log.d(tag, msg)
    }

    fun e(tag: String, msg: String) {
        if (!isDebug)
            return
        val targetStackTraceElement = getTargetStackTraceElement()
        Log.e(tag, "(" + targetStackTraceElement.fileName + ":" + targetStackTraceElement.lineNumber + ")")
        Log.e(tag, msg)
    }

    /************************/
    private fun getTargetStackTraceElement(): StackTraceElement {
        var traceElement: StackTraceElement? = null
        var shouldTrace = false
        val stackTrace: Array<StackTraceElement> = Thread.currentThread().stackTrace

        for (stackTraceElement in stackTrace) {
            val isLogMethod:Boolean=stackTraceElement.className.equals(ZLog.javaClass.name)
            if (shouldTrace && !isLogMethod) {
                traceElement = stackTraceElement
                break
            }
            shouldTrace = isLogMethod
        }
        return traceElement!!
    }


}